import axios from 'axios'
import store from '@/store/index'



// const baseURL = import.meta.env.MODE=='development'?import.meta.env.VITE_BASE_URL :  window.webConfig.BASE_URL

// 
// let baseURL = sessionStorage.getItem('BASE_URL')
let baseURL = window.webConfig.BASE_URL
console.log(baseURL);

import { ElMessage, ElMessageBox } from "element-plus";
const http = {}
var instance = axios.create({

    timeout: 5000,
    baseURL: baseURL,
})
instance.interceptors.request.use(

    function (config) {

        if (store.state.UserToken) {
            config.headers.Authorization = store.state.UserToken
        }
        return config
    },
    function (error) {
        return Promise.reject(error)
    }
)

instance.interceptors.response.use(
    respones => {
        // console.log(respones,'respones')
        return respones.data
    },
    err => {
        if (err && err.respones) {
            // console.log(err.respones,'err')
            switch (err.respones.status) {
                case 400:
                    err.message = '请求出错'
                    break

                case 401:

                    ElMessage({
                        showClose: true,
                        message: "授权失败，请重新登录!",
                        type: "warning",
                    });
                    store.commit('LOGIN_OUT')
                    setTimeout(() => {
                        window.location.reload()
                    }, 1000);
                    break
                case 403:
                    err.message = '拒绝访问'
                    break
                case 404:
                    err.message = '请求错误，未找到服务器'
                    break
                case 500:
                    err.message = '服务器出错'
                    break
            }
        } else {
            err.message = '服务器连接失败'
        }

        ElMessage({
            showClose: true,
            message: err.message,
            type: "error",
        });
        return Promise.reject(err.respones)
    }
)

http.get = function (url, params) {
    console.log(params,8888);
    return new Promise((resolve, reject) => {
        instance.get(url, params).then(response => {
            if (response.code == 0) {
                resolve(response.data)
            } else {

                ElMessage({
                    showClose: true,
                    message: response.message,
                    type: "error",
                });
                reject(response.message)
            }
        }).catch(e => {
            console.log(e);
        })
    })
}
http.post = function (url, data, options) {
    return new Promise((resolve, reject) => {
        instance.post(url, data, options).then(response => {
            if (response.code == 0) {
                resolve(response)
            } else {

                ElMessage({
                    showClose: true,
                    message: response.message,
                    type: "error",
                });
                reject(response.message)
            }
        })
            .catch(e => {
                console.log(e);
            })
    })
}
export default http